﻿using System;
using RikaScript.Libs.Base;

namespace RikaScript.Libs
{
    [Library(Name = "random", Help = "RikaScript 标准随机数类库", Version = "v0.1.0")]
    public class RandomLib : ScriptLibBase
    {
        [Method(Help = "在一个范围中随机返回数据，根据参数区分整数和小数范围")]
        public object range(object start, object end)
        {
            var random = new Random((int) DateTime.Now.Ticks);
            if (ScriptTools.AnyIsDecimal(start, end))
            {
                return random.NextDouble() * (end.Double() - start.Double()) + start.Double();
            }
            else
            {
                return random.Next((int) start.Long(), (int) end.Long());
            }
        }

        [Method(Help = "给定概率返回yes，否则返回no，概率应该是0-1之间的小数")]
        public object where(object probability, object yes, object no)
        {
            var p = probability.Double();
            var random = new Random((int) DateTime.Now.Ticks);
            return p > random.NextDouble() ? yes : no;
        }
    }
}